package com.epfresh.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CenterListActivity;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.activity.MoreEvaluateActivity;
import com.epfresh.activity.WebActivity;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.T;
import com.epfresh.bean.ListGoods;
import com.epfresh.bean.StoreDetail;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseFragment;
import com.epfresh.utils.CommonUtils;
import com.epfresh.views.stick.OnStickyListener;
import com.epfresh.views.stick.StickyScrollView;

import java.util.ArrayList;

/**
 * Created by wzh on 16-11-11.
 */

public class CommonStoreInfoFragment extends BaseFragment implements OnStickyListener {

//    UNCERTIFIED(0, "未认证"),
//    CERTIFYING(1, "认证中"),
//    HAS_CERTIFIED(2, "已认证"),
//    REJECTED(3, "认证失败");
    private TextView tvClass;
    private TextView tvDescription;

    private TextView tvMarket;
    private TextView tvAddress;
    private TextView tvPhone;
    private TextView tvPayment;
    private TextView tvDiscount;
    private TextView tvDelivery;
    //private TextView tvCert;
    private TextView tvRate;
    private TextView tvAmount;
    private View llMoreDc;
    private View llRate;

    private StickyScrollView ssv;


    private StoreDetail storeDetail;

    private TextView tvAcceptTime;

    private boolean isMoreDc;

    public void updateData(StoreDetail storeDetail) {
        this.storeDetail = storeDetail;
        updateStoreStatus();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ssv = (StickyScrollView) inflater.inflate(R.layout.fragment_common_store_info, container, false);

        return ssv;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        tvClass = (TextView) view.findViewById(R.id.tv_class);
        tvDescription = (TextView) view.findViewById(R.id.tv_description);

        tvMarket = (TextView) view.findViewById(R.id.tv_market);
        tvAddress = (TextView) view.findViewById(R.id.tv_address);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        tvPayment = (TextView) view.findViewById(R.id.tv_payment);
        tvDiscount = (TextView) view.findViewById(R.id.tv_discount);
        tvDelivery = (TextView) view.findViewById(R.id.tv_delivery);
        //tvCert = (TextView) view.findViewById(R.id.tv_cert_status);
        tvRate = (TextView) view.findViewById(R.id.tv_rate);
        tvAmount = (TextView) view.findViewById(R.id.tv_amount);
        llRate = view.findViewById(R.id.ll_comment);
        llMoreDc = view.findViewById(R.id.ll_more_dc);

        llMoreDc.setOnClickListener(this);
//        llRate.setOnClickListener(this);

        tvAcceptTime = (TextView) view.findViewById(R.id.tv_accept_time);
        view.findViewById(R.id.iv_rate).setOnClickListener(this);
        view.findViewById(R.id.tv_rate).setOnClickListener(this);
        view.findViewById(R.id.ll_phone).setOnClickListener(this);

        updateStoreStatus();
    }

    public void updateStoreStatus() {
        if (tvMarket != null) {
            if (storeDetail != null) {
                if (storeDetail.isDc()) {
                    llMoreDc.setVisibility(View.VISIBLE);
                } else {
                    llMoreDc.setVisibility(View.GONE);
                }
                tvClass.setText(storeDetail.getMainBusinessCategoryNames());
                tvDescription.setText(storeDetail.getDescription());

                tvMarket.setText(storeDetail.getMarketName());
                tvAddress.setText(storeDetail.getAddress());
                tvPhone.setText(storeDetail.getManagerPhone());
                tvPayment.setText(storeDetail.getPaymentInfo());
                tvDiscount.setText(storeDetail.getDiscount());
                tvDelivery.setText(storeDetail.getDeliveryInfo());
                //tvCert.setText(storeDetail.getCert());
                tvAmount.setText(storeDetail.getOrderCount()+"单");
                tvRate.setText(storeDetail.getGoodCommentsRate()+"好评率");
                String businessTime = storeDetail.getBusinessTime();
                if (businessTime == null || "".equals(businessTime)){
                    tvAcceptTime.setText("00:00-24:00");
                }else {

                    tvAcceptTime.setText(businessTime);
                }
            } else {
                tvMarket.setText("");
                tvAddress.setText("");
                tvPhone.setText("");
                tvPayment.setText("");
                tvDiscount.setText("");
                tvDelivery.setText("");
                //tvCert.setText("");
                tvAmount.setText("");
                tvRate.setText("");
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_more_dc:
                intoMoreDc();

                break;
            case R.id.ll_comment:
                intoComment();

                break;

            case R.id.iv_rate:
            case R.id.tv_rate:
                intoComment();

                break;
            case R.id.ll_phone:
                String managerPhone = storeDetail.getManagerPhone();
                if (managerPhone != null && !"".equals(managerPhone))
                    CommonUtils.intoPhone(getActivity(), managerPhone);

                break;
        }
    }

    private void intoComment() {
        if(storeDetail==null){
            T.toast("数据已过期，请重载应用");
        }
//        Intent more = new Intent(getContext(), MoreEvaluateActivity.class);
//        more.putExtra("storeId", storeDetail.getId());
//        if(storeDetail!=null&&storeDetail.getGoodCommentsRate()!=null&&storeDetail.getGoodCommentsRate().length()>1){
//            String rate = storeDetail.getGoodCommentsRate().substring(0,storeDetail.getGoodCommentsRate().length()-1);
//            more.putExtra("evaluate_percent",rate);
//        }else
//            more.putExtra("evaluate_percent","0");
//        startActivity(more);

        Intent h5Intent = new Intent(getAppActivity(), WebActivity.class);
        h5Intent.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_GOODS_DETAIL_COMMENT + "storeId=" + storeDetail.getId());
        h5Intent.putExtra(Constant.KEY_WEB_TITLE,"评价");
        startActivity(h5Intent);

/*        Intent more = new Intent(getContext(), MoreEvaluateActivity.class);
        if(storeDetail==null){
            T.toast("数据已过期，请重载应用");
        }
        more.putExtra("evaluate_percent",storeDetail.getGoodCommentsRate());
        more.putExtra("storeId", storeDetail.getId());
        startActivity(more);*/
    }

    private void intoMoreDc() {

        Intent tv_more = new Intent(getContext(),
                CenterListActivity.class);
        startActivity(tv_more);
    }

    @Override
    public String getBaseTag() {
        return null;
    }


    @Override
    public boolean isTop() {
        if (ssv != null) {
            Log.e("scroll", "top:" + ssv.isScrolledToTop());
            Log.e("scroll", "bottom:" + ssv.isScrolledToBottom());
            return ssv.isScrolledToTop();
        }
        return false;
    }

    @Override
    public void setScroll(boolean canScroll){
        if (ssv != null){
            ssv.setCanScrolled(canScroll);
        }
    }

}
